package application;
	
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;  
import java.util.logging.Logger;

import base.Manager;
import base.Person;
import base.Staff;
import db.DB_tools;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;  
import javafx.fxml.Initializable;  
import javafx.fxml.JavaFXBuilderFactory;  
import javafx.scene.Scene;  
import javafx.scene.layout.AnchorPane;  
import javafx.stage.Stage;  
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    private Stage stage;
    private final double MINMUM_WIDTH=800.0;
    private final double MINMUM_HEIGHT=600.0;
   
    
	@Override
	public void start(Stage primaryStage) {
		stage = primaryStage;
		stage.setTitle("Cooperation Management System");
		stage.setMinHeight(MINMUM_HEIGHT);
		stage.setMinWidth(MINMUM_WIDTH);
		primaryStage.setResizable(false);
		gotologin();
		stage.show();
	}
	
	public void gotologin(){
		try{
		LoginController login = (LoginController) relpaceSceneContent("Login.fxml");
		login.setApp(this);
		}catch (Exception ex){
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
	
	public void gotoDetailAssignment()
	{
		try{
			detailOfAssignmentController detail = (detailOfAssignmentController) relpaceSceneContent("detailOfAssignment.fxml");
			detail.setApp(this);
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotoAssignment(Person obj)
	{
		try{
			AssignmentForStaffController assignment = (AssignmentForStaffController) relpaceSceneContent("AssignmentForStaff.fxml");
			assignment.setApp(this);
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotoaddAssignment(Manager obj)
	{
		try{
			AssignmentForManagerController assignment = (AssignmentForManagerController) relpaceSceneContent("AssignmentForManager.fxml");
			assignment.setApp(this);
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotoAssignmentList(Manager obj)
	{
		try{
			ShowAssignmentToManagerController assignment = (ShowAssignmentToManagerController) relpaceSceneContent("ShowAssignmentToManager.fxml");
			assignment.setApp(this);
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotoSignup()
	{
		try{
			signUpPageController signup = (signUpPageController) relpaceSceneContent("signUpPage.fxml");
			signup.setApp(this);
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotoNamelist(Manager obj)
	{
		try{
			StaffinfoForManagerController namelist = (StaffinfoForManagerController) relpaceSceneContent("Staffinfo.fxml");
			namelist.setApp(this);
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotoStaffinfo(Person obj){
		try{
			//if(obj instanceof Person){
			StaffinfoController staffinfo = (StaffinfoController) relpaceSceneContent("StaffinfoForStaff.fxml");
			staffinfo.setApp(this);
			staffinfo.showinfo(obj.getName(),obj.getID());
			//}
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotoaddBulletin(Manager obj){
		try{
			if(obj instanceof Manager){
			BulletinForManagerController bulletin = (BulletinForManagerController) relpaceSceneContent("BulletinForManager.fxml");
			bulletin.setApp(this);
			}
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotoBulletin(Person obj){
		try{
			//if(obj instanceof Person){
			BulletinController bulletin = (BulletinController) relpaceSceneContent("Bulletin.fxml");
			bulletin.setApp(this);
			//}
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	private Initializable relpaceSceneContent(String fxml) throws Exception{
		FXMLLoader loader = new FXMLLoader();  
         
        loader.setBuilderFactory(new JavaFXBuilderFactory());  
        loader.setLocation(Main.class.getResource(fxml));  
        InputStream in = Main.class.getResourceAsStream(fxml);
        try{ 
        AnchorPane page = (AnchorPane) loader.load(in);
        Scene scene = new Scene(page, 800, 600);  
        stage.setScene(scene);  
        stage.sizeToScene(); 
        return (Initializable) loader.getController();  
		} finally{
            in.close(); 
		}
         
         
       
	}

	/*public void gotostaffinfo(){
		System.out.println("Success!");
		try{
		SuccessController logout = (SuccessController) relpaceSceneContent("Success.fxml"); 
		logout.setApp(this);
		}catch (Exception ex){
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		}
	}*/
	
	public void userlogin(){
		
	    	gotologin();
	    }
	    
	
	public void userout(){
		gotologin();
	}
	
	public void gotoworkdayinfo(Person obj,String id){
		try{
			AttendanceChartController attendancechart = (AttendanceChartController) relpaceSceneContent("AttendanceChart.fxml");
			attendancechart.setApp(this);
			attendancechart.setPersonData(id);
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotoCheckAttendance(Person obj){
		try{
			CheckAttendanceController checkattendance = (CheckAttendanceController) relpaceSceneContent("CheckAttendance.fxml");
			checkattendance.setApp(this);
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotooffdayinfo(Person obj,String id){
		try{
			AttendanceChart2Controller attendancechart = (AttendanceChart2Controller) relpaceSceneContent("AttendanceChart2.fxml");
			attendancechart.setApp(this);
			attendancechart.setPersonData(id);
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	public void gotonamelist(Manager obj){
		try{
			NamelistController namelist = (NamelistController) relpaceSceneContent("Namelist.fxml");
			namelist.setApp(this);
			}catch (Exception ex){
				Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
			}
	}
	
	
	public static void main(String[] args) {
		launch(args);
	}
}
